/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package triangulate;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
import java.util.ArrayList;

/**
 *
 * @author daniel
 */
public class SVGUtils
    {

    public static ColoredPolygon parseD(String attribute, int width, int height)
        {

        char[] tokens =
            {
            'L', 'M', 'z'
            };
        String work = attribute;
        String current = "";
        String remain = "";
        String coords = "";
        ColoredPolygon pol = new ColoredPolygon();
        int next = 0;
        float x, y;

        while (work.length() > 0)
            {

            next = -1;
            remain = work.substring(1);
            if (remain.indexOf(tokens[0]) > 0)
                {
                next = remain.indexOf(tokens[0]);
                }

            if (remain.indexOf(tokens[1]) > 0 && remain.indexOf(tokens[1]) < next)
                {
                next = remain.indexOf(tokens[1]);
                }

            if (remain.indexOf(tokens[2]) > 0 && remain.indexOf(tokens[2]) < next)
                {
                next = remain.indexOf(tokens[2]);
                }


            if (next != -1)
                {
                current = work.substring(0, next);
                work = work.substring(next).trim();
                }
            else
                {
                current = work.substring(0, work.indexOf(tokens[2])).trim();
                work = "";
                }


            //       System.out.println(current);
            current = current.substring(current.lastIndexOf(" ")).trim();
            x = Float.parseFloat(current.substring(0, current.indexOf(',')));
            y = Float.parseFloat(current.substring(current.indexOf(',') + 1));
            //      System.out.println(String.valueOf(x) + ";" + String.valueOf(y));
            x = x * width / 800;
            y = y * height / 480;
            pol.addPoint((int) x, (int) y);
            }
        //graphics.drawPolygon(pol);
        return pol;
        }

    public static void drawPolygons(Graphics g, ArrayList<ColoredPolygon> ps)
        {
        drawPolygons(g, ps, 0, 0);
        }

    public static void drawPolygons(Graphics g, ArrayList<ColoredPolygon> ps, int x, int y)
        {
        for (int c = 0; c < ps.size();  ++ c)
            {
            drawPolygon(g, ps.get(c), x, y);
            }

        }

    public static void drawPolygon(Graphics g, ColoredPolygon p)
        {
        drawPolygon(g, p, 0, 0);
        }

    static void drawPolygon(Graphics g, ColoredPolygon p, int x, int y)
        {
        ColoredPolygon _p = new ColoredPolygon();

        for (int c = 0; c < p.npoints;  ++ c)
            {
//                g.drawRect( x + p.xpoints[c] - 2, y + p.ypoints[c] - 2, 4, 4);
            _p.addPoint(p.xpoints[c] + x, p.ypoints[c] + y);
            }
        g.setColor(p.color);
        g.fillPolygon(_p);
        }

    public static ColoredPolygon CullXNoMoreThan(
            float aSpan, ColoredPolygon aSrc)
        {
        ColoredPolygon pa = new ColoredPolygon();
        pa.color = aSrc.color;
        pa.originalStyle = aSrc.originalStyle;

        int qtd = aSrc.npoints;
        if (qtd == 0)
            {
            return pa;
            }
        boolean lastinside = (aSrc.xpoints[qtd - 1] < aSpan);
        float DX;
        int prev;
        float a2, b1, b2;
        for (int c = 0; c < qtd; c ++)
            {

            if (aSrc.xpoints[c] < aSpan)
                {


                if ( ! lastinside)
                    {

                    prev = c - 1;
                    while (prev < 0)
                        {
                        prev += qtd;
                        }
                    prev = prev % qtd;

                    a2 = aSrc.xpoints[c] - aSrc.xpoints[prev];
                    b1 = aSrc.xpoints[c] - aSpan;
                    b2 = aSrc.ypoints[c] - aSrc.ypoints[prev];
                    DX = aSrc.ypoints[c] - ((b1 * b2) / a2);

                    pa.addPoint((int) aSpan, (int) DX);
                    lastinside = true;
                    }

                pa.addPoint(aSrc.xpoints[c], aSrc.ypoints[c]);
                }
            else
                {
                if (lastinside)
                    {
                    prev = c - 1;

                    while (prev < 0)
                        {
                        prev += qtd;
                        }
                    prev = prev % qtd;

                    a2 = aSrc.xpoints[c] - aSrc.xpoints[prev];
                    b1 = aSrc.xpoints[c] - aSpan;
                    b2 = aSrc.ypoints[c] - aSrc.ypoints[prev];
                    DX = aSrc.ypoints[c] - ((b1 * b2) / a2);
                    pa.addPoint((int) aSpan, (int) DX);
                    lastinside = false;
                    }
                }
            }
        return pa;

        }

    public static ColoredPolygon CullXNoLessThan(
            float aSpan, ColoredPolygon aSrc)
        {
        ColoredPolygon pa = new ColoredPolygon();
        pa.color = aSrc.color;
        pa.originalStyle = aSrc.originalStyle;
        int qtd = aSrc.npoints;
        if (qtd == 0)
            {
            return pa;
            }
        boolean lastinside = (aSrc.xpoints[qtd - 1] > aSpan);
        float DX;
        int prev;
        float a2, b1, b2;
        for (int c = 0; c < qtd; c ++)
            {
            if (aSrc.xpoints[c] > aSpan)
                {
                if ( ! lastinside)
                    {

                    prev = c - 1;
                    while (prev < 0)
                        {
                        prev += qtd;
                        }
                    prev = prev % qtd;

                    a2 = aSrc.xpoints[c] - aSrc.xpoints[prev];
                    b1 = aSrc.xpoints[c] - aSpan;
                    b2 = aSrc.ypoints[c] - aSrc.ypoints[prev];
                    DX = aSrc.ypoints[c] - ((b1 * b2) / a2);

                    pa.addPoint((int) aSpan, (int) DX);
                    lastinside = true;
                    }
                pa.addPoint(aSrc.xpoints[c], aSrc.ypoints[c]);
                }
            else
                {
                if (lastinside)
                    {
                    prev = c - 1;

                    while (prev < 0)
                        {
                        prev += qtd;
                        }
                    prev = prev % qtd;

                    a2 = aSrc.xpoints[c] - aSrc.xpoints[prev];
                    b1 = aSrc.xpoints[c] - aSpan;
                    b2 = aSrc.ypoints[c] - aSrc.ypoints[prev];
                    DX = aSrc.ypoints[c] - ((b1 * b2) / a2);

                    pa.addPoint((int) aSpan, (int) DX);
                    lastinside = false;
                    }
                }
            }
        return pa;

        }

    public static ColoredPolygon CullYNoMoreThan(
            float aSpan, ColoredPolygon aSrc)
        {
        ColoredPolygon pa = new ColoredPolygon();
        pa.color = aSrc.color;
        pa.originalStyle = aSrc.originalStyle;
        int qtd = aSrc.npoints;
        if (qtd == 0)
            {
            return pa;
            }
        boolean lastinside = (aSrc.ypoints[qtd - 1] < aSpan);
        float DY;
        int prev;
        float a2, b1, b2;
        for (int c = 0; c < qtd; c ++)
            {
            if (aSrc.ypoints[c] < aSpan)
                {


                if ( ! lastinside)
                    {

                    prev = c - 1;
                    while (prev < 0)
                        {
                        prev += qtd;
                        }
                    prev = prev % qtd;

                    a2 = aSrc.ypoints[c] - aSrc.ypoints[prev];
                    b1 = aSrc.ypoints[c] - aSpan;
                    b2 = aSrc.xpoints[c] - aSrc.xpoints[prev];
                    DY = aSrc.xpoints[c] - ((b1 * b2) / a2);
                    pa.addPoint((int) DY, (int) aSpan);
                    lastinside = true;
                    }

                pa.addPoint(aSrc.xpoints[c], aSrc.ypoints[c]);
                }
            else
                {
                if (lastinside)
                    {
                    prev = c - 1;

                    while (prev < 0)
                        {
                        prev += qtd;
                        }
                    prev = prev % qtd;

                    a2 = aSrc.ypoints[c] - aSrc.ypoints[prev];
                    b1 = aSrc.ypoints[c] - aSpan;
                    b2 = aSrc.xpoints[c] - aSrc.xpoints[prev];
                    DY = aSrc.xpoints[c] - ((b1 * b2) / a2);
                    pa.addPoint((int) DY, (int) aSpan);
                    lastinside = false;
                    }
                }
            }
        return pa;
        }

    public static ColoredPolygon CullYNoLessThan(
            float aSpan, ColoredPolygon aSrc)
        {
        ColoredPolygon pa = new ColoredPolygon();
        pa.color = aSrc.color;
        pa.originalStyle = aSrc.originalStyle;
        int qtd = aSrc.npoints;
        if (qtd == 0)
            {
            return pa;
            }
        boolean lastinside = (aSrc.ypoints[qtd - 1] > aSpan);
        float DY;
        int prev;
        float a2, b1, b2;
        for (int c = 0; c < qtd; c ++)
            {
            if (aSrc.ypoints[c] > aSpan)
                {
                if ( ! lastinside)
                    {

                    prev = c - 1;
                    while (prev < 0)
                        {
                        prev += qtd;
                        }
                    prev = prev % qtd;
                    a2 = aSrc.ypoints[c] - aSrc.ypoints[prev];
                    b1 = aSrc.ypoints[c] - aSpan;
                    b2 = aSrc.xpoints[c] - aSrc.xpoints[prev];
                    DY = aSrc.xpoints[c] - ((b1 * b2) / a2);
                    pa.addPoint((int) DY, (int) aSpan);
                    lastinside = true;
                    }

                pa.addPoint(aSrc.xpoints[c], aSrc.ypoints[c]);
                }
            else
                {
                if (lastinside)
                    {
                    prev = c - 1;

                    while (prev < 0)
                        {
                        prev += qtd;
                        }
                    prev = prev % qtd;
                    a2 = aSrc.ypoints[c] - aSrc.ypoints[prev];
                    b1 = aSrc.ypoints[c] - aSpan;
                    b2 = aSrc.xpoints[c] - aSrc.xpoints[prev];
                    DY = aSrc.xpoints[c] - ((b1 * b2) / a2);
                    pa.addPoint((int) DY, (int) aSpan);
                    lastinside = false;
                    }
                }
            }
        return pa;
        }

    static void order(int[] xv, int[] xi)
        {

        ord[] _xi = new ord[xi.length];


        for (int f = 0; f < xi.length;  ++ f)
            {
            _xi[f] = new ord();
            _xi[f].i = xi[f];
            _xi[f].v = xv[f];
            }

        int size = xv.length;
        int menori = 0;
        int menorv = xv[menori];
        ord tmp;

        for (int c = 0; c < size;  ++ c)
            {

            menori = c;
            menorv = _xi[menori].v;

            for (int d = c + 1; d < size;  ++ d)
                {

                if (menorv > _xi[d].v)
                    {

                    menorv = _xi[d].v;
                    menori = d;

                    }

                }

            tmp = _xi[c];
            _xi[c] = _xi[menori];
            _xi[menori] = tmp;
            }

        for (int e = 0; e < size;  ++ e)
            {
            xv[e] = _xi[e].v;
            xi[e] = _xi[e].i;
            }
        }

    static Color parseColorFromStyle(String attribute)
        {
        String fill;

        fill = attribute.substring(attribute.indexOf("#") + 1);
        fill = fill.substring(0, fill.indexOf(";"));
//            System.out.println(" text:" + fill );

        String r = fill.substring(0, 2);
        String g = fill.substring(2, 4);
        String b = fill.substring(4, 6);

//            System.out.println( "r:" + r );
//            System.out.println( "g:" + g );
//            System.out.println( "b:" + b );


        return new Color(Integer.parseInt(r, 16), Integer.parseInt(g, 16), Integer.parseInt(b, 16));
        }
    }
